package com.yunxi.service.dubboService.impl.sto;

import com.yunxi.model.sto.StoVehicleLendOrder;
import com.yunxi.qrtz.service.VehicleLendOrderJobDubboService;
import com.yunxi.service.localService.sto.StoVehicleLendOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 借车单超期提醒 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-06-10
 */

public class VehicleLendOrderJobDubboServiceImpl implements VehicleLendOrderJobDubboService {

    @Autowired
    @Qualifier(value = "stoVehicleLendOrderServiceImpl")
    StoVehicleLendOrderService stoVehicleLendOrderService;


    /**
     * 查询超期的借车单
     */
    @Override
    public void remindOverTimeLendOrder() throws Exception{
        List<StoVehicleLendOrder> list = stoVehicleLendOrderService.queryOverTimeLendOrder();
        if(list != null && list.size() > 0){
            for (StoVehicleLendOrder stoVehicleLendOrder : list){
                //设置是否超期为是
                //取还车日期,写入超期天数
                try {
                    Date returnTime = stoVehicleLendOrder.getReturnTime();
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    returnTime = simpleDateFormat.parse(simpleDateFormat.format(returnTime));//预计归还时间
                    date = simpleDateFormat.parse(simpleDateFormat.format(date));//当前时间
                    int days = (int) ((date.getTime() - returnTime.getTime()) / (1000*3600*24));//超期天数
                    stoVehicleLendOrder.setIsOverTime(1);
                    stoVehicleLendOrder.setOverDays(days);
                    stoVehicleLendOrderService.updateById(stoVehicleLendOrder);
                }
                catch (ParseException e){
                    throw new Exception("操作失败,日期转换异常!");
                }
            }
        }
    }
}
